/**
 * Created by LeungZ on 2016/10/17.
 */
import jsYaml from 'js-yaml';
class Create {
    constructor($state, appImgList, version, service) {
        "ngInject";
        this.fData = {};
        if (appImgList) {
            this.appImgList = appImgList.list;
        }

        if ($state.params.k8sCode) {
            this.fData.cluster = {
                clusterCode: $state.params.k8sCode
            };
        }

        if ($state.params.appId) {
            this.fData.app = {
                id: $state.params.appId,
               
            }
        }

        if ($state.params.serId) {
            this.fData.services = [{
                serviceId: $state.params.serId,
                baseCtrl: {
                    podAppName: service.serviceCode
                },
                advCtrl: {
                    podDomain: service.url
                },
                 namespace:$state.params.namespace
            }];
            console.log(this.fData.services[0].namespace,'create')
        }

        if ($state.params.verId) {
            this.fData.services[0].versionId = $state.params.verId;
        }

        if (version) {
            var spec = jsYaml.safeLoad(version.yamlContent).spec;

            this.fData.services[0].rcName = version.rcName;
            this.fData.services[0].baseCtrl = {
                appImg: {
                    imageNameAndVersion: version.imageNameAndVersion,
                    ver: {
                        id: version.imageIndexId
                    }
                },
                podAppName: version.serviceCode,
                podNum: spec.replicas
            };

            spec = spec.template.spec;
            if (spec.containers[0].resources && spec.containers[0].resources.limits) {
                this.fData.services[0].baseCtrl.podCpu = spec.containers[0].resources.limits.cpu;
                this.fData.services[0].baseCtrl.podMem = spec.containers[0].resources.limits.memory.substr(0, spec.containers[0].resources.limits.memory.length - 1);
            }
            if (spec.containers[0].volumeMounts && spec.containers[0].volumeMounts.length) {
                this.fData.services[0].baseCtrl.volumes = [];
                this.fData.services[0].baseCtrl.logVolumes = [];
                angular.forEach(spec.containers[0].volumeMounts, (vol, index)=> {
                    if (spec.volumes[index].hostPath.path === '/home/pshizh/logs/') {
                        this.fData.services[0].baseCtrl.logVolumes.push({
                            name: vol.name,
                            path: vol.mountPath
                        });
                    } else {
                        this.fData.services[0].baseCtrl.volumes.push({
                            name: vol.name,
                            mountPath: vol.mountPath,
                            hostPath: spec.volumes[index].hostPath.path
                        });
                    }

                });
            }
            if (spec.nodeSelector) {
                this.fData.services[0].baseCtrl.groups = [];
                angular.forEach(Object.keys(spec.nodeSelector), item=> {
                    this.fData.services[0].baseCtrl.groups.push(item);
                });
            }
        }

        if ($state.current.name != "app-service-version-publish.deploy") {
            $state.go('app-service-version-publish.deploy');
        }
    }
}
export default Create